/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;

/**
 * <p>Project: app2022 - Ex7
 * <p>Powered by webrx On 2022-01-18 15:57:37
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex7 {
    public static void main(String[] args) {
        var src = new File("d:\\download\\Windows11_x642.iso");
        var dst = new File("d:\\download\\Windows11_x64.iso");
        if (isSame(src, dst)) {
            System.out.println("是同一个文件");
        } else {
            System.out.println("不是");
        }
    }

    /**
     * Description: 判断两个文件是不是同一个文件
     *
     * @date: 2022/1/18 16:16
     * @params: [java.lang.String, java.lang.String]
     * @return: boolean
     */
    public static boolean isSame(String src, String dst) {
        return isSame(new File(src), new File(src));
    }

    /**
     * Description: 返回md5字符串32位，byte[] buf 内容有一个字节，返回的字符串
     *
     * @date: 2022/1/18 16:27
     * @params:
     * @return:
     */
    public static String md5(byte[] buf) {
        StringBuilder s = new StringBuilder();
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            md.update(buf);
            for (byte b : md.digest()) {
                s.append(String.format("%02x", b));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s.toString();
    }

    public static boolean isSame(File src, File dst) {
        boolean f = false;
        if (src.length() == dst.length()) {
            try (var f1 = new FileInputStream(src); var f2 = new FileInputStream(dst)) {
                //f = md5(f1.readAllBytes()).equals(md5(f2.readAllBytes()));

                //certutil -hashfile [文件路径] MD5
                /*
                D:\download>certutil -hashfile Windows11_x64.iso MD5
                    MD5 的 Windows11_x64.iso 哈希:
                    e0821103af4215098090786811cc9b75
                    CertUtil: -hashfile 命令成功完成。

                    D:\download>certutil -hashfile Windows11_x642.iso MD5
                    MD5 的 Windows11_x642.iso 哈希:
                    e0821103af4215098090786811cc9b75
                    CertUtil: -hashfile 命令成功完成。
                 */
                //apache commons codec 生成FileInputStream 文件流的md5码
                f = DigestUtils.md5Hex(f1).equals(DigestUtils.md5Hex(f2));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;
    }

    /**
     * Description: 判断两个文件是不是同一个文件
     *
     * @date: 2022/1/18 16:01
     * @params: [java.io.File, java.io.File]
     * @return: boolean
     */
    public static boolean isSame(File src, File dst, boolean is) {
        boolean f = false;
        if (src.length() == dst.length()) {
            try (var f1 = new FileInputStream(src); var f2 = new FileInputStream(dst)) {
                long temp = 0;
                while (f1.read() == f2.read()) {
                    ++temp;
                    if (temp == src.length()) {
                        break;
                    }
                }
                if (temp == src.length()) {
                    f = true;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;
    }
}
